<template>
  <div class="login-vue">
    <div class="login-container" v-loading="loading">
      <div class="login-content">
        <div class="login-title">{{ $Glob.LOGIN_TITLE }}</div>
        <div class="login-name login-input">
          <input
            type="text"
            name="username"
            v-model="account"
            autocomplete="off"
          />
          <span
            class="placeholder"
            :class="{ isFixed: account != '' && account != null }"
            >用户名</span
          >
        </div>
        <div class="login-password login-input">
          <input
            type="password"
            name="password"
            v-model="password"
            @keyup.enter="submit"
            autocomplete="off"
          />
          <span
            class="placeholder"
            :class="{ isFixed: password != '' && password != null }"
            >密码</span
          >
        </div>
        <div class="buttonDiv">
          <el-button class="dl_but" type="primary" @click="submit"
            >登录</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import $P from "../../js/common/public";
import $Req from "../../js/common/request";
import $S from "../../js/common/storage";
export default {
  data() {
    return {
      account: "",
      password: "",
      loading: false,
      routeObj: [],
    };
  },
  created() {
    const token = $S.Get("token") || "";
    if (token.length > 0) {
      this.$router.push({ path: "/" + ($S.Get("home") || "home") });
    }
  },
  mounted() {},
  methods: {
    submit() {
      const self = this;
      if ($P.Invalid(self.account)) {
        $P.Msg("用户名不能为空！", 3);
        return false;
      }

      if ($P.Invalid(self.password)) {
        $P.Msg("密码不能为空！", 3);
        return false;
      }

      if (self.password.length <= 2 || self.password.length > 20) {
        $P.Msg("密码长度为 3-20！", 3);
        return false;
      }

      self.loading = true;

      // 密码md5处理
      $Req.Admin.adminLogin({
        params: Object.assign(
          { username: self.account },
          { password: self.password }
        ),
        success(res) {
          //登录成功，先清除所有信息.
          self.account = "";
          self.password = "";
          $S.Clear();

          //再记录账号信息
          $S.Set("token", res.token);
          $S.Set("menu", res.menu);
          $S.Set("menuList", res.menuList);
          $S.Set("home", res.home);
          $S.Set("nike", res.nike);
          $S.Set("username", res.username);
          self.loading = false;
          self.$router.push({ path: "/" + res.home });
        },
        newEnd() {
          self.loading = false;
        },
        error() {
          self.loading = false;
        },
      });
    },
  },
};
</script>

<style scoped>
.login-vue {
  text-align: center;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  padding: 0px;
  margin: 0px;
  white-space: nowrap;
  background-color: #106f8e;
  background-image: url(../../static/img/main_bg.png);
  background-position: center 0;
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

.login-container {
  width: 340px;
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}

.login-content {
  letter-spacing: 2px;
  background: #fff;
  padding: 30px 30px 20px;
  -webkit-box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.06);
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.06);
  border-radius: 3px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

.login-content > div {
  margin: 40px 0;
}

.login-title {
  font-size: 30px;
  color: #3a3a3a;
  line-height: 1;
  font-weight: 200;
}

div.login-input {
  position: relative;
}

div.login-input input {
  font-size: 16px;
  padding: 8px 0;
  height: 40px;
  width: 100%;
  border-radius: 0;
  border: none;
  border-bottom: 0.5px solid #d3d3d3;
  -webkit-box-shadow: inset 0 0 0 1000px #fff;
  box-shadow: inset 0 0 0 1000px #fff;
  outline: none;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  -webkit-transition: 0.3s;
  transition: 0.3s;
  font-weight: 200;
}

button,
input {
  overflow: visible;
}

div.login-input .placeholder {
  position: absolute;
  color: #7e7e7e;
  top: 6px;
  font-size: 16px;
  -webkit-transition: all 0.2s;
  transition: all 0.2s;
  left: 0;
  pointer-events: none;
}

div.login-input input:focus {
  border-bottom-color: #3788ee;
  box-shadow: inset 0 0 0 1000px #fff;
}

input:-webkit-autofill + .placeholder {
  font-size: 13px;
  top: -16px;
}

input:focus + .placeholder,
.placeholder.isFixed {
  font-size: 13px !important;
  top: -16px !important;
}

.buttonDiv {
  margin-top: 45px;
  margin-bottom: 15px;
}

.dl_but {
  width: 100%;
  padding: 12px 0;
  font-size: 18px;
  opacity: 0.8;
  border-radius: 3px;
  margin-top: 15px;
}

.dl_but:hover {
  opacity: 1;
  background: #3788ee;
  border-color: #3788ee;
}
</style>